/**
 * Please modify this class to meet your needs
 * This class is not complete
 */

package com.cctv.eip.messagingplatformservice._0_1;

import java.util.Arrays;
import java.util.List;

import com.cctv.eip.messagingplatformservice._0.ResponseType;
import com.ctvit.common.ConstantsSms;
import com.ctvit.common.EntityClassSet;
import com.ctvit.framework.web.context.ContextHolder;
import com.ctvit.sms.platform.domain.Person;
import com.ctvit.sms.platform.service.CustomMsgService;
import com.ctvit.struts.common.CommonFunc;
import com.ctvit.wsservice.basedata.domain.SmsMember;
import com.ctvit.wsservice.basedata.domain.ZcdmTSms;
import com.ctvit.wsservice.basedata.service.EIPSendMessagingService;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

/**
 * This class was generated by Apache CXF 3.1.4 2016-01-21T16:48:59.310+08:00
 * Generated source version: 3.1.4
 */

@javax.jws.WebService(serviceName = "EIPMessagingPlatformService", portName = "EIPSendMessagingPort", targetNamespace = "http://cctv.com/eip/messagingPlatformService/0.1", endpointInterface = "com.cctv.eip.messagingplatformservice._0_1.EIPMessagePortType")
public class EIPSendMessagingPortImpl implements EIPMessagePortType {

	private static final Logger LOG = Logger
			.getLogger(EIPSendMessagingPortImpl.class);

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.cctv.eip.messagingplatformservice._0_1.EIPMessagePortType#getServiceInfo
	 * (com.cctv.esb.schema.monitor._0.GetServiceInfoRequestType
	 * getServiceInfoRequest )*
	 */
	public com.cctv.esb.schema.monitor._0.GetServiceInfoResponseType getServiceInfo(
			com.cctv.esb.schema.monitor._0.GetServiceInfoRequestType getServiceInfoRequest) {
		LOG.info("Executing operation getServiceInfo");
		System.out.println(getServiceInfoRequest);
		try {
			com.cctv.esb.schema.monitor._0.GetServiceInfoResponseType _return = null;
			return _return;
		} catch (java.lang.Exception ex) {
			ex.printStackTrace();
			throw new RuntimeException(ex);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.cctv.eip.messagingplatformservice._0_1.EIPMessagePortType#sendMessage
	 * (com.cctv.esb.schema.messagehead._0.RequestHeadType requestHead
	 * ,)com.cctv.eip.messagingplatformservice._0.MessageCommonDataType
	 * messageRequest )*
	 */
	public com.cctv.eip.messagingplatformservice._0.ResponseType sendMessage(
			com.cctv.esb.schema.messagehead._0.RequestHeadType requestHead,
			com.cctv.eip.messagingplatformservice._0.MessageCommonDataType messageRequest) {
		LOG.info("Executing operation sendMessage");
		String response = "";
		ZcdmTSms zcdmTSms = null;
		EntityClassSet entityClassSet = new EntityClassSet();
		EIPSendMessagingService eipService = ContextHolder
				.getBean(EIPSendMessagingService.class);
		CustomMsgService customMsgService = ContextHolder
				.getBean(CustomMsgService.class);
		com.cctv.eip.messagingplatformservice._0.ResponseType _return = null;

		// interface check start *******************************************************************************************************
		String memberCode = messageRequest.getAppBidType();
		String recvs = messageRequest.getRecvsType();
		String title = messageRequest.getTitleType();
		String content = messageRequest.getContentType();
		Integer type = 0;
		Boolean isValid = true;
		try {
			type = Integer.parseInt(messageRequest.getMessageType());
		} catch (NumberFormatException e) {
			LOG.error("操作类型格式错误：" + e.getMessage(), e.getCause());
			isValid = false;
		}
		if (!ConstantsSms.OPERAS.contains(type)) {
			LOG.error("操作类型不支持：:" + type);
			isValid = false;
		}
		SmsMember member = eipService.getByMemberCode(memberCode);
		if (member == null) {
			LOG.error("应用系统编码未找到：" + memberCode);
			isValid = false;
		} else if (0 == member.getStatus()) {
			LOG.error("应用系统已禁用：" + memberCode);
			isValid = false;
		}
		if (!StringUtils.isNotEmpty(recvs)) {
			LOG.error("消息接收者为空：" + memberCode);
			isValid = false;
		}
		List<String> receivers = Arrays.asList(recvs.split(","));
		if (receivers.size() > 100) {
			LOG.error("消息接收者超过100人：" + recvs);
			isValid = false;
		}

		for (String receiver : receivers) {
			if (receiver.length() != 8) {
				LOG.error("消息接收者UID不是8位码：" + receiver);
				isValid = false;
			}
		}
		//			//判断用户短信是否超过限额
		//			//得到月限量是否超过限额true表示不超过
		//			boolean  monthJudge = memberMessageService.findmonth(memberCode);
		//			System.out.println("---月限额--"+monthJudge);
		//			if(!monthJudge){
		//				LOG.error("已经超过月限额：" + monthJudge);
		//    			response.setStatus("false");
		//        		return response;
		//			}
		//			//得到年限额是否超过限额
		//			boolean  yearJudge = memberMessageService.findyear(memberCode);
		//			System.out.println("---年限额--"+yearJudge);
		//			if(!yearJudge){
		//				LOG.error("已经超过年限额：" + yearJudge);
		//    			response.setStatus("false");
		//        		return response;
		//			}
		//
		// interface check over *******************************************************************************************************

		// recvs check start *******************************************************************************************************
		/**
		 * 检查接收人的ID是否有效
		 * 是否有手机号
		 */
		Boolean hasInvalidPerson = false;     //是否有无效的人员
		Boolean hasInvalidityMobile = false;     //是否有无效的手机号
		Boolean hasValidPerson = false;           //是否有有效的人员
		Boolean hasValidMobile = false;           //是否有有效的手机号
		String[] uids = recvs.split(",");

		for (int i = 0; i < uids.length; i++) {
			Person person = customMsgService.getPersonInfo(uids[i]);
			//先查人存不存在
			if (person != null && org.apache.commons.lang3.StringUtils.isNotEmpty(person.getZPerno())) {
				hasValidPerson = true;
				//人存在的话 再查手机号是否存在
				String mobile = person.getZMobph();
				if (org.apache.commons.lang3.StringUtils.isNotEmpty(mobile) && CommonFunc.isMobileValid(mobile)) {
					//是有效的手机号
					hasValidMobile = true;
				} else {
					hasInvalidityMobile = true;
					LOG.error("人员:" + uids[0] + "的手机号验证失败:" + mobile);
				}
			} else {
				hasInvalidPerson = true;
				LOG.error("未在person表中找到人员:" + uids[0]);
			}
		}
		if (hasValidMobile == false) {     //如果一个验证通过的手机号都没有
			isValid = false;
			LOG.error("一个验证通过的手机号都没有");
		}
		// recvs check over *******************************************************************************************************

		try {
			zcdmTSms = entityClassSet.setZcdmTSms(requestHead, messageRequest,
					zcdmTSms);
			if (isValid == true) {       //检查没问题 记正常状态
				zcdmTSms.setZStatus("0");
			} else {
				zcdmTSms.setZStatus("9");
			}
			Long pk = eipService.saveSMS(zcdmTSms);// 保存信息
			zcdmTSms.setId(pk);

			if (isValid == true) {       //检查没问题 执行发送
				eipService.insertSmsExtract(zcdmTSms);
				eipService.doExecProc(pk);

				response = "ok";
			} else {
				response = "false";
			}
			_return = setResponseType(response);
			return _return;
		} catch (java.lang.Exception ex) {
			ex.printStackTrace();
			throw new RuntimeException(ex);
		}
	}

	/**
	 * @param response
	 * @return
	 * @ps:设置返回信息
	 */
	private ResponseType setResponseType(String response) {
		ResponseType responseType = new ResponseType();
		responseType.setStatus(response);
		return responseType;
	}
}
